package com.blt.govdata.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.blt.govdata.controller.base.BaseController;
import com.blt.govdata.core.constants.Constants;
import com.blt.govdata.model.TDictionary;
import com.blt.govdata.model.vo.TDictionaryVo;
import com.blt.govdata.service.ITDictionaryService;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 * @author AustinGJ123
 * @since 2018-03-27
 */
@RestController
@RequestMapping("/tDictionary")
//todo 将所有的XXX修改为真实值
public class TDictionaryController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ITDictionaryService service;

    /**
     * 分页查询字典
     * size:分页大小
     * current:当前页
     */
    @RequestMapping("/page")
    public Map<String, Object> getUsersPage(@RequestParam Map<String, Object> params, Page<TDictionaryVo> page) {
        params.put("parentId", 0);
        Page<TDictionaryVo> result = this.service.selectDictionaryPage(page, params);
        List<TDictionaryVo> list = result.getRecords();
        if (CollectionUtils.isEmpty(list)) return ImmutableMap.of("status", "true", "data", Lists.newArrayList());
        Iterator it = list.iterator();
        while (it.hasNext()) {
            TDictionaryVo vo = (TDictionaryVo) it.next();
            vo.setChildren(this.service.selectList(BeanUtils.instantiate(EntityWrapper.class).where("parent_id = {0}", vo.getId())
                    .and("is_delete = {0}", Constants.UN_DELETED)
                    .orderBy("sort")));
        }

        logger.info("分页查询字典");
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 新增字典
     */
    @PostMapping("/addDictionary")
    public Map<String, Object> addRole(TDictionary dictionary) {
        boolean result = this.service.insert(dictionary);
        logger.info("新增字典");
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 编辑字典
     */
    @PostMapping("/editDictionary")
    public Map<String, Object> editDictionary(TDictionary dictionary) {
        boolean result = this.service.updateById(dictionary);
        logger.info("编辑字典");
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 删除字典
     */
    @PostMapping("/deleteDictionary")
    public Map<String, Object> deleteRole(TDictionary dictionary) {
        if (null == dictionary || dictionary.getId() == null) return ImmutableMap.of("status", "true", "data", false);
        dictionary.setIsDelete(Constants.UN_DELETED); //逻辑删除
        boolean result = this.service.updateById(dictionary);
        logger.info("删除字典");
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 根据Id查询字典
     */
    @RequestMapping(value = "/selectDictionaryById", params = {"id"})
    public Map<String, Object> selectDictionaryById(Integer id) {
        return ImmutableMap.of("status", "true", "data", this.service.selectById(id));
    }

    /**
     * 通过Code查询字典的ID和name字段
     */
    @RequestMapping(value = "/selectDictionaryByCode", params = {"code"})
    public Map<String, Object> selectDictionaryById(String code) {
        List<Map<String, Object>> list = this.service.selectMaps(BeanUtils.instantiate(EntityWrapper.class).where("code = {0}", code)
                .setSqlSelect("id", "name")
                .and("parent_id != {0}", 0)
                .and("is_delete = {0}", Constants.UN_DELETED));
        return ImmutableMap.of("status", "true", "data", list);
    }

}
